/*
 * Date         Author       Note
 * 2014.11.24   舞者无罪              程序编辑窗口
 * 2015.04.24   wzwz         纱嘴控件的状态要反过来
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include <mh/common.h>
#include<mh/com.h>
#include "resource.h"
#include "func.h"
#include "mhlabel.h"
#include "mhbtn.h"
#include "mhedit.h"
#include "mhctrlstate.h"
#include "lang.h"
#include "program.h"
#include "com.h"
#include "knit_line.h"
#include "utf8_gb2312.h"
#include "flower.h"
#include "keypane.h"
#include "act.h"
#include "rock_drt.h"
#include "rock_num.h"
#include "info.h"
#include "dlg_ask.h"

#define LINE_CAPTION_LEFT   (CX_EDGE_LEFT)
#define LINE_CAPTION_TOP    (5)
#define LINE_CAPTION_WIDTH  (40)
#define LINE_CAPTION_HEIGHT (20)
#define LINE_NAME_WIDTH     (100)

#define POS_BTN_LEFT        LINE_CAPTION_LEFT
#define POS_BTN_WIDTH       70
#define POS_BTN_HEIGHT      30
#define POS_BTN_TOP         (CY_SCREEN - POS_BTN_HEIGHT - 10)
#define POS_BTN_INTER       (10)

#define DUMU1_CAPTION_LEFT      LINE_CAPTION_LEFT
#define DUMU1_CAPTION_TOP      (CY_SCREEN - 200)
#define DUMU1_CAPTION_WIDTH    (70)
#define DUMU1_CAPTION_HEIGHT   (20)
#define DUMU1_CAPTION_INTER    (5)
#define DUMU1_NAME_WIDTH       (30)
#define DUMU1_NAME_INTER       (10)

#define FB_CAPTION_LEFT        (CX_EDGE_LEFT * 2)
#define FB_CAPTION_TOP         (LINE_CAPTION_TOP + LINE_CAPTION_HEIGHT * 2)
#define FB_CAPTION_WIDTH       (50)
#define FB_CAPTION_HEIGHT      (24)
#define FB_CAPTION_RIGHT       (CX_SCREEN - CX_EDGE_LEFT * 2)
#define CX_FB_INTER            (5)
#define CY_FB_INTER            (12)
/*
 * read control page
 * param line: 1-start
 */
static void ReadControlPage(HWND hWnd, ushort line)
{
    HWND hTmp = -1;
    int i = 0;
    KNIT_PAGE *p = (KNIT_PAGE*)GetControlPage(g_knit_data, line);
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    if(data.pwnd_program)
    {
        if(p)
        {
            memcpy(&data.pwnd_program->con, p, sizeof(KNIT_PAGE));
        }
        else
        {
            memset(&data.pwnd_program->con, 0, sizeof(KNIT_PAGE));
        }
        p = &data.pwnd_program->con;
    }
    else
    {
        p = NULL;
    }
    if(p)
    {
        int drt = line & 1;//0 - to left, 1 - to right
        char sc_value[256] = {0};
        sprintf(sc_value, "%u", drt ? p->flw_2B : p->flw_1B);
        SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT_BACKL_ROW), sc_value);
        GetCmdString(sc_value, drt ? p->act_2BH : p->act_1BH, TRUE);
        SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT_LEFT_CMD_BH), sc_value);
        GetCmdString(sc_value, drt ? p->act_2BA : p->act_1BA, FALSE);
        SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT_LEFT_CMD_BA), sc_value);

        sprintf(sc_value, "%u", drt ? p->flw_1B : p->flw_2B);
        SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT_BACKR_ROW), sc_value);
        GetCmdString(sc_value, drt ? p->act_1BH : p->act_2BH, TRUE);
        SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT_RGHT_CMD_BH), sc_value);
        GetCmdString(sc_value, drt ? p->act_1BA : p->act_2BA, FALSE);
        SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT_RGHT_CMD_BA), sc_value);

        sprintf(sc_value, "%u", drt ? p->flw_2F : p->flw_1F);
        SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT_FRNTL_ROW), sc_value);
        GetCmdString(sc_value, drt ? p->act_2FA : p->act_1FA, FALSE);
        SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT_LEFT_CMD_FA), sc_value);
        GetCmdString(sc_value, drt ? p->act_2FH : p->act_1FH, TRUE);
        SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT_LEFT_CMD_FH), sc_value);

        sprintf(sc_value, "%u", drt ? p->flw_1F : p->flw_2F);
        SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT_FRNTR_ROW), sc_value);
        GetCmdString(sc_value, drt ? p->act_1FA : p->act_2FA, FALSE);
        SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT_RGHT_CMD_FA), sc_value);
        GetCmdString(sc_value, drt ? p->act_1FH : p->act_2FH, TRUE);
        SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT_RGHT_CMD_FH), sc_value);

        //code and yarn
        for(i = 0; i < 16; ++i)
        {
            hTmp = GetDlgItem(hWnd, ID_BACKL_CODE_H + i);
            ushort code = drt ? p->code_2BH : p->code_1BH;
            code &= 1 << i;
            SetCtrlState(hTmp, code ? TRUE : FALSE);
            InvalidateRect(hTmp, NULL, TRUE);

            hTmp = GetDlgItem(hWnd, ID_BACKL_CODE_A + i);
            code = drt ? p->code_2BA : p->code_1BA;
            code &= 1 << i;
            SetCtrlState(hTmp, code ? TRUE : FALSE);
            InvalidateRect(hTmp, NULL, TRUE);

            hTmp = GetDlgItem(hWnd, ID_BACKR_CODE_H + i);
            code = drt ? p->code_1BH : p->code_2BH;
            code &= 1 << i;
            SetCtrlState(hTmp, code ? TRUE : FALSE);
            InvalidateRect(hTmp, NULL, TRUE);

            hTmp = GetDlgItem(hWnd, ID_BACKR_CODE_A + i);
            code = drt ? p->code_1BA : p->code_2BA;
            code &= 1 << i;
            SetCtrlState(hTmp, code ? TRUE : FALSE);
            InvalidateRect(hTmp, NULL, TRUE);

            hTmp = GetDlgItem(hWnd, ID_FRONTL_CODE_A + i);
            code = drt ? p->code_2FA : p->code_1FA;
            code &= 1 << i;
            SetCtrlState(hTmp, code ? TRUE : FALSE);
            InvalidateRect(hTmp, NULL, TRUE);

            hTmp = GetDlgItem(hWnd, ID_FRONTL_CODE_H + i);
            code = drt ? p->code_2FH : p->code_1FH;
            code &= 1 << i;
            SetCtrlState(hTmp, code ? TRUE : FALSE);
            InvalidateRect(hTmp, NULL, TRUE);

            hTmp = GetDlgItem(hWnd, ID_FRONTR_CODE_A + i);
            code = drt ? p->code_1FA : p->code_2FA;
            code &= 1 << i;
            SetCtrlState(hTmp, code ? TRUE : FALSE);
            InvalidateRect(hTmp, NULL, TRUE);

            hTmp = GetDlgItem(hWnd, ID_FRONTR_CODE_H + 1);
            code = drt ? p->code_1FH : p->code_2FH;
            code &= 1 << i;
            SetCtrlState(hTmp, code ? TRUE : FALSE);
            InvalidateRect(hTmp, NULL, TRUE);

            if(i < 8)
            {
                code = drt ? p->gauze2_1 : p->gauze1_1;
                code &= 0x80 >> i;
                hTmp = GetDlgItem(hWnd, ID_LEFT_1YARN + i);
                SetCtrlState(hTmp, code ? FALSE : TRUE);
                InvalidateRect(hTmp, NULL, TRUE);
            }
            else
            {
                code = drt ? p->gauze1_1 : p->gauze2_1;
                code &= 0x80 >> (i - 8);
                hTmp = GetDlgItem(hWnd, ID_RGHT_1YARN + (i - 8));
                SetCtrlState(hTmp, code ? FALSE : TRUE);
                InvalidateRect(hTmp, NULL, TRUE);
            }
        }

        sprintf(sc_value, "%u", p->dumu1);
        SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT_DUMU1), sc_value);
        sprintf(sc_value, "%u", p->dumu2);
        SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT_DUMU2), sc_value);
        sprintf(sc_value, "%u", p->high_roll);
        SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT_MAST_ROLL), sc_value);
        sprintf(sc_value, "%u", p->low_roll);
        SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT_AUX_ROLL), sc_value);

        sprintf(sc_value, "%u", p->speed);
        SetWindowCaption(GetDlgItem(hWnd, IDC_SPEED), sc_value);

        GetRockString(sc_value, p->rock.drt, p->rock.needle, p->rock.flag);
        if(p->rock2_num.needle || p->rock2_dir.drt || p->rock2_dir.flag)
        {
            GetRockString(&sc_value[strlen(sc_value)], p->rock2_dir.drt,
                    p->rock2_num.needle, p->rock2_dir.flag);
        }
        if(IS_TRANSFER(p->act_1BA) || IS_TRANSFER(p->act_1FA) || IS_TRANSFER(
                p->act_2BA) || IS_TRANSFER(p->act_2FA))
        {
            strcat(sc_value, "t");
        }
        SetWindowCaption(GetDlgItem(hWnd, IDC_ROCK), sc_value);

        sprintf(sc_value, "%u", p->stop);
        SetWindowCaption(GetDlgItem(hWnd, IDC_YARN_STOP), sc_value);

        sprintf(sc_value, "%u", p->sink);
        SetWindowCaption(GetDlgItem(hWnd, IDC_SINK), sc_value);
        SendMessage(GetDlgItem(hWnd, ID_END), BM_SETCHECK, p->end.end ? TRUE : FALSE, 0);
    }
}
/*
 *
 */
static void FlushPrgWndRock(HWND hWnd, KNIT_PAGE *p)
{
    char sc_value[128] = { 0 };
    GetRockString(sc_value, p->rock.drt, p->rock.needle, p->rock.flag);
    if(p->rock2_num.needle || p->rock2_dir.drt || p->rock2_dir.flag)
    {
        GetRockString(&sc_value[strlen(sc_value)], p->rock2_dir.drt,
                p->rock2_num.needle, p->rock2_dir.flag);
    }
    if(IS_TRANSFER(p->act_1BA) || IS_TRANSFER(p->act_1FA) || IS_TRANSFER(
            p->act_2BA) || IS_TRANSFER(p->act_2FA))
    {
        strcat(sc_value, "t");
    }
    HWND hTmp = GetDlgItem(hWnd, IDC_ROCK);
    SetWindowCaption(hTmp, sc_value);
    InvalidateRect(hTmp, NULL, TRUE);
}
/*
 *
 */
static void CreateProgramInterface(HWND hWnd)
{
    int i, j;
    char sc[MH_FILE_NAME_MAX_LEN + 32] = {0};
    ushort curt_line = GetCurrentKnitLine(g_knit_data);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_SCREEN - CX_ASK_BMP ,
        2,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
    CreateWindow(MH_LABEL_NAME,
        MW_LINE_NUM_CAPTION,
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        LINE_CAPTION_LEFT,
        LINE_CAPTION_TOP,
        LINE_CAPTION_WIDTH,
        LINE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_4GROUP);
    sprintf(sc, "%d/%d", curt_line, GetTotalKnitPage(g_knit_data));
    CreateWindow(MH_LABEL_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_LINE_FINISHED,
        LINE_CAPTION_LEFT + LINE_CAPTION_WIDTH,
        LINE_CAPTION_TOP,
        LINE_NAME_WIDTH,
        LINE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_4GROUP);
    char name[MH_FILE_NAME_MAX_LEN] = {0};
    const char *psc = GetKnitFileName(g_knit_data);
    UTF_8ToGB2312(name, psc, strlen(psc) + 1);
    sprintf(sc, MW_PROGRAM_EDIT_CAPTION, name);
    CreateWindow(MH_LABEL_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_FILE_NAME,
        CX_SCREEN / 2 - 200,
        LINE_CAPTION_TOP,
        400,
        LINE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_4GROUP);
    //left back
    CreateWindow (MH_LABEL_NAME,
        "Back",
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        FB_CAPTION_LEFT,
        FB_CAPTION_TOP,
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_6GROUP);
    //left back line
    CreateWindow (MH_LABEL_NAME,
        MW_ROW_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        FB_CAPTION_LEFT + FB_CAPTION_WIDTH,
        FB_CAPTION_TOP,
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "0",
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_BACKL_ROW,
        FB_CAPTION_LEFT + (FB_CAPTION_WIDTH * 2 + CX_FB_INTER),
        FB_CAPTION_TOP,
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BORDER_BLACK | MH_EDIT_BKGND_WHITE | MH_EDIT_TEXT_BLACK);
    //H
    CreateWindow (MH_LABEL_NAME,
        "H",
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        FB_CAPTION_LEFT + FB_CAPTION_WIDTH,
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT + CY_FB_INTER),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_BTN_NAME,
        "",
        WS_CHILD | WS_VISIBLE | BS_FLAT,
        IDC_EDIT_LEFT_CMD_BH,
        FB_CAPTION_LEFT + (FB_CAPTION_WIDTH * 2 + CX_FB_INTER),
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT + CY_FB_INTER),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_BTN_BKGND_GREEN | MH_BTN_TEXT_BLACK | MH_BTN_BLACK_BORDER);
    //A
    CreateWindow (MH_LABEL_NAME,
        "A",
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        FB_CAPTION_LEFT + FB_CAPTION_WIDTH,
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 3 + CY_FB_INTER),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_BTN_NAME,
        "",
        WS_CHILD | WS_VISIBLE | BS_FLAT,
        IDC_EDIT_LEFT_CMD_BA,
        FB_CAPTION_LEFT + (FB_CAPTION_WIDTH * 2 + CX_FB_INTER),
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 3 + CY_FB_INTER),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_BTN_BKGND_GREEN | MH_BTN_TEXT_BLACK | MH_BTN_BLACK_BORDER);
    //Yarn
    CreateWindow (MH_LABEL_NAME,
        MW_YARN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        FB_CAPTION_LEFT + FB_CAPTION_WIDTH,
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT + CY_FB_INTER) * 4,
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    //cam
    CreateWindow (MH_LABEL_NAME,
        MW_SYS1_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        FB_CAPTION_LEFT,
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT + CY_FB_INTER) * 4,
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_7GROUP);
    //A
    CreateWindow (MH_LABEL_NAME,
        "A",
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        FB_CAPTION_LEFT + FB_CAPTION_WIDTH,
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 6 + CY_FB_INTER * 5),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_BTN_NAME,
        "",
        WS_CHILD | WS_VISIBLE | BS_FLAT,
        IDC_EDIT_LEFT_CMD_FA,
        FB_CAPTION_LEFT + (FB_CAPTION_WIDTH * 2 + CX_FB_INTER),
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 6 + CY_FB_INTER * 5),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_BTN_BKGND_GREEN | MH_BTN_TEXT_BLACK | MH_BTN_BLACK_BORDER);
    //H
    CreateWindow (MH_LABEL_NAME,
        "H",
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        FB_CAPTION_LEFT + FB_CAPTION_WIDTH,
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 7 + CY_FB_INTER * 7),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_BTN_NAME,
        "",
        WS_CHILD | WS_VISIBLE | BS_FLAT,
        IDC_EDIT_LEFT_CMD_FH,
        FB_CAPTION_LEFT + (FB_CAPTION_WIDTH * 2 + CX_FB_INTER),
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 7 + CY_FB_INTER * 7),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_BTN_BKGND_GREEN | MH_BTN_TEXT_BLACK | MH_BTN_BLACK_BORDER);
    //left front line
    CreateWindow (MH_LABEL_NAME,
        MW_ROW_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        FB_CAPTION_LEFT + FB_CAPTION_WIDTH,
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 9 + CY_FB_INTER * 8),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "0",
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_FRNTL_ROW,
        FB_CAPTION_LEFT + (FB_CAPTION_WIDTH * 2 + CX_FB_INTER),
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 9 + CY_FB_INTER * 8),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BORDER_BLACK | MH_EDIT_BKGND_WHITE | MH_EDIT_TEXT_BLACK);
    //left front
    CreateWindow (MH_LABEL_NAME,
        "Front",
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        FB_CAPTION_LEFT,
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 9 + CY_FB_INTER * 8),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_6GROUP);
    //right back
    CreateWindow (MH_LABEL_NAME,
        "Back",
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        FB_CAPTION_RIGHT - FB_CAPTION_WIDTH,
        FB_CAPTION_TOP,
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_6GROUP);
    //left back line
    CreateWindow (MH_LABEL_NAME,
        MW_ROW_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        CX_SCREEN / 2 + FB_CAPTION_LEFT + FB_CAPTION_WIDTH,
        FB_CAPTION_TOP,
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "0",
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_BACKR_ROW,
        CX_SCREEN / 2 + FB_CAPTION_LEFT + (FB_CAPTION_WIDTH * 2 + CX_FB_INTER),
        FB_CAPTION_TOP,
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BORDER_BLACK | MH_EDIT_BKGND_WHITE | MH_EDIT_TEXT_BLACK);
    //H
    CreateWindow (MH_LABEL_NAME,
        "H",
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        CX_SCREEN / 2 + FB_CAPTION_LEFT + FB_CAPTION_WIDTH,
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT + CY_FB_INTER),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_BTN_NAME,
        "",
        WS_CHILD | WS_VISIBLE | BS_FLAT,
        IDC_EDIT_RGHT_CMD_BH,
        CX_SCREEN / 2 + FB_CAPTION_LEFT + (FB_CAPTION_WIDTH * 2 + CX_FB_INTER),
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT + CY_FB_INTER),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_BTN_BKGND_GREEN | MH_BTN_TEXT_BLACK | MH_BTN_BLACK_BORDER);
    //A
    CreateWindow (MH_LABEL_NAME,
        "A",
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        CX_SCREEN / 2 + FB_CAPTION_LEFT + FB_CAPTION_WIDTH,
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 3 + CY_FB_INTER),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_BTN_NAME,
        "",
        WS_CHILD | WS_VISIBLE | BS_FLAT,
        IDC_EDIT_RGHT_CMD_BA,
        CX_SCREEN / 2 + FB_CAPTION_LEFT + (FB_CAPTION_WIDTH * 2 + CX_FB_INTER),
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 3 + CY_FB_INTER),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_BTN_BKGND_GREEN | MH_BTN_TEXT_BLACK | MH_BTN_BLACK_BORDER);
    //Yarn
    CreateWindow (MH_LABEL_NAME,
        MW_YARN_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        CX_SCREEN / 2 + FB_CAPTION_LEFT + FB_CAPTION_WIDTH,
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT + CY_FB_INTER) * 4,
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    //cam
    CreateWindow (MH_LABEL_NAME,
        MW_SYS2_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        FB_CAPTION_RIGHT - FB_CAPTION_WIDTH,
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT + CY_FB_INTER) * 4,
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_7GROUP);
    //A
    CreateWindow (MH_LABEL_NAME,
        "A",
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        CX_SCREEN / 2 + FB_CAPTION_LEFT + FB_CAPTION_WIDTH,
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 6 + CY_FB_INTER * 5),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_BTN_NAME,
        "",
        WS_CHILD | WS_VISIBLE | BS_FLAT,
        IDC_EDIT_RGHT_CMD_FA,
        CX_SCREEN / 2 + FB_CAPTION_LEFT + (FB_CAPTION_WIDTH * 2 + CX_FB_INTER),
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 6 + CY_FB_INTER * 5),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_BTN_BKGND_GREEN | MH_BTN_TEXT_BLACK | MH_BTN_BLACK_BORDER);
    //H
    CreateWindow (MH_LABEL_NAME,
        "H",
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        CX_SCREEN / 2 + FB_CAPTION_LEFT + FB_CAPTION_WIDTH,
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 7 + CY_FB_INTER * 7),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_BTN_NAME,
        "",
        WS_CHILD | WS_VISIBLE | BS_FLAT,
        IDC_EDIT_RGHT_CMD_FH,
        CX_SCREEN / 2 + FB_CAPTION_LEFT + (FB_CAPTION_WIDTH * 2 + CX_FB_INTER),
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 7 + CY_FB_INTER * 7),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_BTN_BKGND_GREEN | MH_BTN_TEXT_BLACK | MH_BTN_BLACK_BORDER);
    //right front line
    CreateWindow (MH_LABEL_NAME,
        MW_ROW_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        CX_SCREEN / 2 + FB_CAPTION_LEFT + FB_CAPTION_WIDTH,
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 9 + CY_FB_INTER * 8),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "0",
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_FRNTR_ROW,
        CX_SCREEN / 2 + FB_CAPTION_LEFT + (FB_CAPTION_WIDTH * 2 + CX_FB_INTER),
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 9 + CY_FB_INTER * 8),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BORDER_BLACK | MH_EDIT_BKGND_WHITE | MH_EDIT_TEXT_BLACK);
    //right front
    CreateWindow (MH_LABEL_NAME,
        "Front",
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        FB_CAPTION_RIGHT - FB_CAPTION_WIDTH,
        FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 9 + CY_FB_INTER * 8),
        FB_CAPTION_WIDTH,
        FB_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_6GROUP);
    char *scCode[16] = {"0", "1", "2", "3", "4", "5", "6", "7",
        "8", "9", "A", "B", "C", "D", "E", "F"
    };
    int topCode[4] = {
            FB_CAPTION_TOP + FB_CAPTION_HEIGHT + 5,
            FB_CAPTION_TOP + FB_CAPTION_HEIGHT * 3 + 5,
            FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 6 + CY_FB_INTER * 5) - 8,
            FB_CAPTION_TOP + (FB_CAPTION_HEIGHT * 8 + CY_FB_INTER * 4) + 3
    };
    int leftCode[2] = {
            FB_CAPTION_LEFT + (FB_CAPTION_WIDTH * 3 + CX_FB_INTER * 2),
            CX_SCREEN / 2 + (FB_CAPTION_WIDTH * 3 + CX_FB_INTER * 6) + 6,
    };
    int idCode[8] = {
            ID_BACKL_CODE_H,
            ID_BACKR_CODE_H,
            ID_BACKL_CODE_A,
            ID_BACKR_CODE_A,
            ID_FRONTL_CODE_A,
            ID_FRONTR_CODE_A,
            ID_FRONTL_CODE_H,
            ID_FRONTR_CODE_H
    };
    for(i = 0; i < 8; ++i)
    {
        for(j = 0; j < 16; ++j)
        {
            MH_CTRL_STATE *ctrl = NEW_MH_CTRL_STATE();
            ctrl->type = MH_CTRL_TYPE_CODE;
            CreateWindow (MH_CTRL_STATE_NAME,
                 scCode[j],
                 WS_CHILD | WS_VISIBLE,
                 idCode[i] + j,
                 leftCode[i & 1] + (j & 7) * 20,
                 topCode[i >> 1] + (j >> 3) * 20,
                 20,
                 20,
                 hWnd, (int)ctrl);
        }
    }
    char *scYarn[8] = {
        "1", "2", "3", "4", "5", "6", "7", "8"
    };
    int idYarn[2] = {
        ID_LEFT_1YARN,
        ID_RGHT_1YARN
    };
    int leftYarn[2] = {
        FB_CAPTION_LEFT + FB_CAPTION_WIDTH * 2 + 10,
        CX_SCREEN / 2 + FB_CAPTION_LEFT + FB_CAPTION_WIDTH * 2 + 10
    };
    for(i = 0; i < 2; ++i)
    {
        for(j = 0; j < 8; ++j)
        {
            MH_CTRL_STATE *ctrl = NEW_MH_CTRL_STATE();
            ctrl->type = MH_CTRL_TYPE_YARN;
            CreateWindow (MH_CTRL_STATE_NAME,
                scYarn[j],
                WS_CHILD | WS_VISIBLE,
                idYarn[i] + j,
                leftYarn[i] + j * FB_CAPTION_HEIGHT,
                FB_CAPTION_TOP + (FB_CAPTION_HEIGHT + CY_FB_INTER) * 4,
                FB_CAPTION_HEIGHT,
                FB_CAPTION_HEIGHT,
                hWnd, (int)ctrl);
        }
    }
    //dumu-roll
    CreateWindow (MH_LABEL_NAME,
        MW_RUN_DUMU1_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        DUMU1_CAPTION_LEFT,
        DUMU1_CAPTION_TOP,
        DUMU1_CAPTION_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "0",
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_DUMU1,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER),
        DUMU1_CAPTION_TOP,
        DUMU1_NAME_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BKGND_BLUE | MH_EDIT_BORDER_GREY | MH_EDIT_TEXT_BLACK);
    CreateWindow (MH_LABEL_NAME,
        MW_RUN_DUMU2_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        DUMU1_CAPTION_LEFT,
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER),
        DUMU1_CAPTION_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "0",
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_DUMU2,
        DUMU1_CAPTION_LEFT + DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER,
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER),
        DUMU1_NAME_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BKGND_BLUE | MH_EDIT_BORDER_GREY | MH_EDIT_TEXT_BLACK);
    CreateWindow (MH_LABEL_NAME,
        MW_MAST_ROLL_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        DUMU1_CAPTION_LEFT,
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER) * 2,
        DUMU1_CAPTION_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "0",
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_MAST_ROLL,
        DUMU1_CAPTION_LEFT + DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER,
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER) * 2,
        DUMU1_NAME_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BKGND_BLUE | MH_EDIT_BORDER_GREY | MH_EDIT_TEXT_BLACK);
    CreateWindow (MH_LABEL_NAME,
        MW_AUX_ROLL_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        DUMU1_CAPTION_LEFT,
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER) * 3,
        DUMU1_CAPTION_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "0",
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_AUX_ROLL,
        DUMU1_CAPTION_LEFT + DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER,
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER) * 3,
        DUMU1_NAME_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BKGND_BLUE | MH_EDIT_BORDER_GREY | MH_EDIT_TEXT_BLACK);
    //speed-stop
    CreateWindow (MH_LABEL_NAME,
        MW_SPEED_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER),
        DUMU1_CAPTION_TOP,
        DUMU1_CAPTION_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "0",
        WS_CHILD | WS_VISIBLE,
        IDC_SPEED,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER) + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER),
        DUMU1_CAPTION_TOP,
        DUMU1_NAME_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BKGND_BLUE | MH_EDIT_BORDER_GREY | MH_EDIT_TEXT_BLACK);
    CreateWindow (MH_LABEL_NAME,
        MW_ROCK_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER),
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER),
        DUMU1_CAPTION_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "0",
        WS_CHILD | WS_VISIBLE,
        IDC_ROCK,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER) + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER),
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER),
        50,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BKGND_BLUE | MH_EDIT_BORDER_GREY | MH_EDIT_TEXT_BLACK);
    CreateWindow (MH_LABEL_NAME,
        MW_SPEED_ROCK_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER),
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER) * 2,
        DUMU1_CAPTION_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        IDC_ROCK_SPEED,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER) + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER),
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER) * 2,
        DUMU1_NAME_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BKGND_BLUE | MH_EDIT_BORDER_GREY | MH_EDIT_TEXT_BLACK);
    CreateWindow (MH_LABEL_NAME,
        MW_YARN_STOP_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER),
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER) * 3,
        DUMU1_CAPTION_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "0",
        WS_CHILD | WS_VISIBLE,
        IDC_YARN_STOP,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER) + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER),
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER) * 3,
        DUMU1_NAME_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BKGND_BLUE | MH_EDIT_BORDER_GREY | MH_EDIT_TEXT_BLACK);
    //ret len-intasia
    CreateWindow (MH_LABEL_NAME,
        MW_RET_INTER_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER) * 2,
        DUMU1_CAPTION_TOP,
        DUMU1_CAPTION_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        IDC_RET_LEN,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER) * 2 + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER),
        DUMU1_CAPTION_TOP,
        DUMU1_NAME_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BKGND_BLUE | MH_EDIT_BORDER_GREY | MH_EDIT_TEXT_BLACK);
    CreateWindow (MH_LABEL_NAME,
        MW_SINK_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER) * 2,
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER),
        DUMU1_CAPTION_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "0",
        WS_CHILD | WS_VISIBLE,
        IDC_SINK,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER) * 2 + DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER,
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER),
        DUMU1_NAME_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BKGND_BLUE | MH_EDIT_BORDER_GREY | MH_EDIT_TEXT_BLACK);
    CreateWindow (MH_LABEL_NAME,
        MW_DUMU_BEFORE_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER) * 2,
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER) * 2,
        DUMU1_CAPTION_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        IDC_DUMU_BEFORE,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER) * 2 + DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER,
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER) * 2,
        DUMU1_NAME_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BKGND_BLUE | MH_EDIT_BORDER_GREY | MH_EDIT_TEXT_BLACK);
    CreateWindow (MH_LABEL_NAME,
        MW_INTASIA_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER) * 2,
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER) * 3,
        DUMU1_CAPTION_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        IDC_INTASIA,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER) * 2 + DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER,
        DUMU1_CAPTION_TOP + (DUMU1_CAPTION_HEIGHT + DUMU1_CAPTION_INTER) * 3,
        DUMU1_NAME_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BKGND_BLUE | MH_EDIT_BORDER_GREY | MH_EDIT_TEXT_BLACK);
    //dmbz
    CreateWindow (MH_LABEL_NAME,
        MW_DMBZ_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER) * 3,
        DUMU1_CAPTION_TOP,
        DUMU1_CAPTION_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (MH_EDIT_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        IDC_DMBZ,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER) * 3 + DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER,
        DUMU1_CAPTION_TOP,
        DUMU1_NAME_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_EDIT_BKGND_BLUE | MH_EDIT_BORDER_GREY | MH_EDIT_TEXT_BLACK);
    //end
    CreateWindow (MH_LABEL_NAME,
        MW_END_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_RIGHT,
        IDC_STATIC,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER) * 4,
        DUMU1_CAPTION_TOP,
        DUMU1_CAPTION_WIDTH,
        DUMU1_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_5GROUP);
    CreateWindow (CTRL_BUTTON,
        "",
        WS_CHILD | BS_CHECKBOX | WS_VISIBLE,
        ID_END,
        DUMU1_CAPTION_LEFT + (DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER + DUMU1_NAME_WIDTH + DUMU1_NAME_INTER) * 4 + DUMU1_CAPTION_WIDTH + DUMU1_CAPTION_INTER,
        DUMU1_CAPTION_TOP,
        15,
        15,
        hWnd, 0);
    //F1_F6
    CreateWindow (MH_BTN_NAME,
        "F1 PAT",
        WS_CHILD | WS_VISIBLE,
        ID_BTN_FLW,
        POS_BTN_LEFT,
        POS_BTN_TOP,
        POS_BTN_WIDTH,
        POS_BTN_HEIGHT,
        hWnd, MH_BTN_TEXT_GREEN);
    CreateWindow (MH_BTN_NAME,
        MW_PROGRAM_F2_CAPTION,
        WS_CHILD | WS_VISIBLE,
        ID_GOTO_LINE,
        POS_BTN_LEFT + (POS_BTN_WIDTH + POS_BTN_INTER),
        POS_BTN_TOP,
        POS_BTN_WIDTH,
        POS_BTN_HEIGHT,
        hWnd, MH_BTN_TEXT_GREEN);
    CreateWindow (MH_BTN_NAME,
        MW_PROGRAM_F3_CAPTION,
        WS_CHILD | WS_VISIBLE,
        ID_START_LINE,
        POS_BTN_LEFT + (POS_BTN_WIDTH + POS_BTN_INTER) * 2,
        POS_BTN_TOP,
        POS_BTN_WIDTH,
        POS_BTN_HEIGHT,
        hWnd, MH_BTN_TEXT_GREEN);
    CreateWindow (MH_BTN_NAME,
        MW_PROGRAM_F4_CAPTION,
        WS_CHILD | WS_VISIBLE,
        ID_STOP_LINE,
        POS_BTN_LEFT + (POS_BTN_WIDTH + POS_BTN_INTER) * 3,
        POS_BTN_TOP,
        POS_BTN_WIDTH,
        POS_BTN_HEIGHT,
        hWnd, MH_BTN_TEXT_GREEN);
    CreateWindow (MH_BTN_NAME,
        MW_PROGRAM_F6_CAPTION,
        WS_CHILD | WS_VISIBLE,
        ID_ROCK,
        POS_BTN_LEFT + (POS_BTN_WIDTH + POS_BTN_INTER) * 4,
        POS_BTN_TOP,
        POS_BTN_WIDTH,
        POS_BTN_HEIGHT,
        hWnd, MH_BTN_TEXT_GREEN);
    CreateWindow (MH_BTN_NAME,
        MW_PROGRAM_PU_CAPTION,
        WS_CHILD | WS_VISIBLE,
        ID_UP,
        POS_BTN_LEFT + (POS_BTN_WIDTH + POS_BTN_INTER) * 5,
        POS_BTN_TOP,
        POS_BTN_WIDTH,
        POS_BTN_HEIGHT,
        hWnd, MH_BTN_TEXT_GREEN);
    CreateWindow (MH_BTN_NAME,
        MW_PROGRAM_PD_CAPTION,
        WS_CHILD | WS_VISIBLE,
        ID_DOWN,
        POS_BTN_LEFT + (POS_BTN_WIDTH + POS_BTN_INTER) * 6,
        POS_BTN_TOP,
        POS_BTN_WIDTH,
        POS_BTN_HEIGHT,
        hWnd, MH_BTN_TEXT_GREEN);
    ReadControlPage(hWnd, curt_line);
    InvalidateRect(hWnd, NULL, FALSE);
}
/*
 *
 */
static void FlushPrgWndLine(HWND hWnd, ushort line)
{
    char sc[256] = { 0 };
    ushort total = GetTotalKnitPage(g_knit_data);
    sprintf(sc, "%d/%d", line, total);
    SetWindowCaption(GetDlgItem(hWnd, IDC_LINE_FINISHED), sc);
    ReadControlPage(hWnd, line);
    InvalidateRect(hWnd, NULL, FALSE);
}
/*
 * get current line of program edit
 * return value: 0-failed
 */
static ushort GetCurrentLine(HWND hWnd)
{
    const char *sc = GetWindowCaption(GetDlgItem(hWnd, IDC_LINE_FINISHED));
    ushort ret = 0;
    if(sc)
    {
        int len = strcspn (sc, "/");
        if(len > 0)
        {
            char *str = calloc(1, len + 1);
            memcpy(str, sc, len);
            ret = atoi(str);
            free(str);
        }
    }
    return ret;
}
/*
 *
 */
static void OnCreateKeyboard(HWND hWnd, ushort nID)
{
    int min = 1;
    int max = 32;
    switch(nID)
    {
    case IDC_EDIT_DUMU1:
        break;
    case IDC_EDIT_DUMU2:
        break;
    case IDC_EDIT_MAST_ROLL:
        break;
    case IDC_EDIT_AUX_ROLL:
        break;
    case IDC_SPEED:
        break;
    case IDC_YARN_STOP:
        {
            min = 1;
            max = 8;
        }
        break;
    case IDC_SINK:
        {
            min = 1;
            max = 4;
        }
        break;
    case IDC_ROCK:
        {
            min = 0;
            max = 15;
        }
        break;
    }
    HWND hTmp = GetDlgItem(hWnd, nID);
    RECT rect;
    GetClientRect(hTmp, &rect);
    ClientToScreen(hTmp, &rect.left, &rect.top);
    ClientToScreen(hTmp, &rect.right, &rect.bottom);
    CreateKeyboardWindow(hWnd, rect, min, max, atoi(GetWindowCaption(hTmp)));
}
/*
 *
 */
static int SavePrgLine(HWND hWnd, ushort line)
{
    int ret = 0;
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    BOOL bWrite = FALSE;
    KNIT_PAGE *p = (KNIT_PAGE*)GetControlPage(g_knit_data, line);
    if(p && data.pwnd_program)
    {
        memcpy(p, &data.pwnd_program->con, sizeof(KNIT_PAGE));
        SendControlLine(g_knit_data, line);
        bWrite = TRUE;
        data.pwnd_program->change = FALSE;
    }
    if(bWrite)
    {
        char sc[MH_FILE_NAME_MAX_LEN + sizeof(KNIT_FILE_DIR) + sizeof(".con")] = { 0 };
        strcat(sc, KNIT_FILE_DIR);
        strcat(sc, GetKnitFileName(g_knit_data));
        strcat(sc, ".con");
        ret = WriteConFile(sc, g_knit_data);
    }
    return ret;
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, ushort nID)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    ushort line = GetCurrentLine(hWnd);
    KNIT_PAGE *p = NULL;
    if(data.pwnd_program)
    {
        p = &data.pwnd_program->con;
    }
	switch(nID)
	{
	case ID_ESC:
	    {
	        if(data.pwnd_program->change)
	        {
	            int ret = 0;
	            if(AfxMessageBox(hWnd, NULL, GetString(585)))
	            {
	                ret = SavePrgLine(hWnd, line);
	            }
	            if(ret)
	            {
	                AfxInfoBox(hWnd, NULL, GetString(481));
	            }
	        }
	    	PostMessage(hWnd, MSG_CLOSE, 0, 0);
	    }
		break;
	case ID_UP:
	    {
	        if(data.pwnd_program->change)
	        {
	            if(AfxMessageBox(hWnd, NULL, GetString(585)))
	            {
	                if(SavePrgLine(hWnd, line))
	                {
	                    AfxInfoBox(hWnd, NULL, GetString(481));
	                }
	            }
	        }
	        if(line > 1)
	        {
	            --line;
	            FlushPrgWndLine(hWnd, line);
	        }
	    }
	    break;
	case ID_DOWN:
	    {
	        if(data.pwnd_program->change)
            {
                if(AfxMessageBox(hWnd, NULL, GetString(585)))
                {
                    if(SavePrgLine(hWnd, line))
                    {
                        AfxInfoBox(hWnd, NULL, GetString(481));
                    }
                }
            }
	        ushort total = GetTotalKnitPage(g_knit_data);
	        if(line < total)
	        {
	            ++line;
	            FlushPrgWndLine(hWnd, line);
	        }
	    }
	    break;
	case (ID_LEFT_1YARN)...(ID_LEFT_8YARN):
	    {
	        int n = nID - ID_LEFT_1YARN;
	        if(p)
	        {
	            if(line & 1)
	            {
	                p->gauze2_1 ^= 0x80 >> n;
	            }
	            else
	            {
	                p->gauze1_1 ^= 0x80 >> n;
	            }
	            data.pwnd_program->change = TRUE;
	        }
	    }
	    break;
	case (ID_RGHT_1YARN)...(ID_RGHT_8YARN):
        {
	        int n = nID - ID_RGHT_1YARN;
	        if(p)
	        {
	            if(line & 1)
	            {
	                p->gauze1_1 ^= 0x80 >> n;
	            }
	            else
	            {
	                p->gauze2_1 ^= 0x80 >> n;
	            }
	            data.pwnd_program->change = TRUE;
	        }
        }
	    break;
	case (ID_BACKL_CODE_H)...(ID_BACKL_CODE_H + 15):
	    if(p)
        {
	        int n = nID - ID_BACKL_CODE_H;
	        if(line & 1)
	        {
	            p->code_2BH ^= 1 << n;
	        }
	        else
	        {
	            p->code_1BH ^= 1 << n;
	        }
        }
	    break;
	case (ID_BACKL_CODE_A)...(ID_BACKL_CODE_A + 15):
	    if(p)
	    {
	        int n = nID - ID_BACKL_CODE_A;
	        if(line & 1)
	        {
	            p->code_2BA ^= 1 << n;
	        }
	        else
	        {
	            p->code_1BA ^= 1 << n;
	        }
	        data.pwnd_program->change = TRUE;
	    }
	    break;
	case (ID_FRONTL_CODE_A)...(ID_FRONTL_CODE_A + 15):
	    if(p)
	    {
	        int n = nID - ID_FRONTL_CODE_A;
	        if(line & 1)
	        {
	            p->code_2FA ^= 1 << n;
	        }
	        else
	        {
	            p->code_1FA ^= 1 << n;
	        }
	        data.pwnd_program->change = TRUE;
	    }
	    break;
	case (ID_FRONTL_CODE_H)...(ID_FRONTL_CODE_H + 15):
	    if(p)
	    {
	        int n = nID - ID_FRONTL_CODE_H;
	        if(line & 1)
	        {
	            p->code_2FH ^= 1 << n;
	        }
	        else
	        {
	            p->code_1FH ^= 1 << n;
	        }
	        data.pwnd_program->change = TRUE;
	    }
	    break;
	case (ID_BACKR_CODE_H)...(ID_BACKR_CODE_H + 15):
	    if(p)
	    {
	        int n = nID - ID_BACKR_CODE_H;
	        if(line & 1)
	        {
	            p->code_1BH ^= 1 << n;
	        }
	        else
	        {
	            p->code_2BH ^= 1 << n;
	        }
	        data.pwnd_program->change = TRUE;
	    }
	    break;
	case (ID_BACKR_CODE_A)...(ID_BACKR_CODE_A + 15):
	    if(p)
	    {
	        int n = nID - ID_BACKR_CODE_A;
	        if(line & 1)
	        {
	            p->code_1BA ^= 1 << n;
	        }
	        else
	        {
	            p->code_1BH ^= 1 << n;
	        }
	        data.pwnd_program->change = TRUE;
	    }
	    break;
	case (ID_FRONTR_CODE_A)...(ID_FRONTR_CODE_A + 15):
	    if(p)
	    {
	        int n = nID - ID_FRONTR_CODE_A;
	        if(line & 1)
	        {
	            p->code_1FA ^= 1 << n;
	        }
	        else
	        {
	            p->code_2FA ^= 1 << n;
	        }
	        data.pwnd_program->change = TRUE;
	    }
	    break;
	case (ID_FRONTR_CODE_H)...(ID_FRONTR_CODE_H + 15):
	    if(p)
	    {
	        int n = nID = ID_FRONTR_CODE_H;
	        if(line & 1)
	        {
	            p->code_1FH ^= 1 << n;
	        }
	        else
	        {
	            p->code_2FH ^= 1 << n;
	        }
	        data.pwnd_program->change = TRUE;
	    }
	    break;
	case ID_END:
	    if(p)
	    {
	        p->end.end ^= 1;
	        data.pwnd_program->change = TRUE;
	    }
	    break;
	case ID_BTN_FLW:
	    {
	        CreateFlowerWindow(hWnd);
	    }
	    break;
	case ID_GOTO_LINE:
	    {
	        CreateKnitLineWindow(hWnd);
	    }
	    break;
	case ID_START_LINE:
	    {
	        //ushort line = GetCurrentLine(hWnd);
            if(line > 1)
            {
                FlushPrgWndLine(hWnd, 1);
            }
	    }
	    break;
	case ID_STOP_LINE:
	    {
	        //ushort line = GetCurrentLine(hWnd);
	        ushort total = GetTotalKnitPage(g_knit_data);
            if(line > 0 && line < total)
            {
                FlushPrgWndLine(hWnd, total);
            }
	    }
	    break;
	case ID_ROCK:
	    if(p)
	    {
	        CreateRockDrtWindow(hWnd, p->rock.drt, p->rock.flag);
	    }
	    break;
	case IDC_EDIT_LEFT_CMD_BH:
	    if(p)
	    {
	        uchar act = 0;
	        if(line & 1)
	        {
	            act = p->act_2BH;
	        }
	        else
	        {
	            act = p->act_1BH;
	        }
	        CreateActWindow(hWnd, act);
	    }
	    break;
    case IDC_EDIT_LEFT_CMD_BA:
        if(p)
        {
            uchar act = 0;
            if(line & 1)
            {
                act = p->act_2BA;
            }
            else
            {
                act = p->act_1BA;
            }
            CreateActWindow(hWnd, act);
        }
        break;
    case IDC_EDIT_LEFT_CMD_FH:
        if(p)
        {
            uchar act = 0;
            if(line & 1)
            {
                act = p->act_2FH;
            }
            else
            {
                act = p->act_1FH;
            }
            CreateActWindow(hWnd, act);
        }
        break;
    case IDC_EDIT_LEFT_CMD_FA:
        if(p)
        {
            uchar act = 0;
            if(line & 1)
            {
                act = p->act_2FA;
            }
            else
            {

                act = p->act_1FA;
            }
            CreateActWindow(hWnd, act);
        }
        break;
    case IDC_EDIT_RGHT_CMD_BH:
        if(p)
        {
            uchar act = 0;
            if(line & 1)
            {
                act = p->act_1BH;
            }
            else
            {
                act = p->act_2BH;
            }
            CreateActWindow(hWnd, act);
        }
        break;
    case IDC_EDIT_RGHT_CMD_BA:
        if(p)
        {
            uchar act = 0;
            if(line & 1)
            {
                act = p->act_1BA;
            }
            else
            {
                act = p->act_2BA;
            }
            CreateActWindow(hWnd, act);
        }
        break;
    case IDC_EDIT_RGHT_CMD_FH:
        if(p)
        {
            uchar act = 0;
            if(line & 1)
            {
                act = p->act_1FH;
            }
            else
            {
                act = p->act_2FH;
            }
            CreateActWindow(hWnd, act);
        }
        break;
    case IDC_EDIT_RGHT_CMD_FA:
        if(p)
        {
            uchar act = 0;
            if(line & 1)
            {
                act = p->act_1FA;
            }
            else
            {
                act = p->act_2FA;
            }
            CreateActWindow(hWnd, act);
        }
        break;
	}
}
/*
 * set edit value
 */
static void OnSetKnitValue(HWND hWnd, ushort nID, ushort value)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    ushort line = GetCurrentLine(hWnd);
    KNIT_PAGE *p = NULL;
    if(data.pwnd_program)
    {
        p = &data.pwnd_program->con;
        data.pwnd_program->change = TRUE;
    }
    switch(nID)
    {
	case IDC_EDIT_BACKL_ROW:
	    if(p)
	    {
	        const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
	        if(sc)
	        {
	            if(line & 1)
	            {
	                p->flw_2B = atoi(sc);
	            }
	            else
	            {
	                p->flw_1B = atoi(sc);
	            }
	        }
	    }
	    break;
	case IDC_EDIT_FRNTL_ROW:
	    if(p)
	    {
	        const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
	        if(sc)
	        {
	            if(line & 1)
	            {
	                p->flw_2F = atoi(sc);
	            }
	            else
	            {
	                p->flw_1F = atoi(sc);
	            }
	        }
	    }
	    break;
	case IDC_EDIT_BACKR_ROW:
	    if(p)
	    {
	        const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
	        if(sc)
	        {
	            if(line & 1)
	            {
	                p->flw_1B = atoi(sc);
	            }
	            else
	            {
	                p->flw_2B = atoi(sc);
	            }
	        }
	    }
	    break;
	case IDC_EDIT_FRNTR_ROW:
	    if(p)
	    {
	        const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
	        if(sc)
	        {
	            if(line & 1)
	            {
	                p->flw_1F = atoi(sc);
	            }
	            else
	            {
	                p->flw_2F = atoi(sc);
	            }
	        }
	    }
	    break;
	case IDC_EDIT_LEFT_CMD_BH:
	case IDC_EDIT_LEFT_CMD_BA:
	    if(p)
	    {
	        char sc[32] = {0};
	        if(line & 1)
	        {
	            p->act_2BH = value;
	            p->act_2BA = value;
	        }
	        else
	        {
	            p->act_1BH = value;
	            p->act_1BA = value;
	        }
	        HWND hWndEdit = GetDlgItem(hWnd, IDC_EDIT_LEFT_CMD_BH);
	        if(hWndEdit != HWND_INVALID)
	        {
	            GetCmdString(sc, value, TRUE);
	            SetWindowCaption(hWndEdit, sc);
	            InvalidateRect(hWndEdit, NULL, TRUE);
	        }
	        hWndEdit = GetDlgItem(hWnd, IDC_EDIT_LEFT_CMD_BA);
	        if(hWndEdit != HWND_INVALID)
	        {
	            GetCmdString(sc, value, FALSE);
	            SetWindowCaption(hWndEdit, sc);
	            InvalidateRect(hWndEdit, NULL, TRUE);
	        }
	    }
	    break;
    case IDC_EDIT_LEFT_CMD_FH:
	case IDC_EDIT_LEFT_CMD_FA:
	    if(p)
	    {
	        char sc[32] = {0};
	        if(line & 1)
	        {
	            p->act_2FH = value;
	            p->act_2FA = value;
	        }
	        else
	        {
	            p->act_1FH = value;
	            p->act_1FA = value;
	        }
	        HWND hWndEdit = GetDlgItem(hWnd, IDC_EDIT_LEFT_CMD_FH);
	        if(hWndEdit != HWND_INVALID)
	        {
	            GetCmdString(sc, value, TRUE);
	            SetWindowCaption(hWndEdit, sc);
	            InvalidateRect(hWndEdit, NULL, TRUE);
	        }
	        hWndEdit = GetDlgItem(hWnd, IDC_EDIT_LEFT_CMD_FA);
	        if(hWndEdit != HWND_INVALID)
	        {
	            GetCmdString(sc, value, FALSE);
	            SetWindowCaption(hWndEdit, sc);
	            InvalidateRect(hWndEdit, NULL, TRUE);
	        }
	    }
	    break;
    case IDC_EDIT_RGHT_CMD_BH:
	case IDC_EDIT_RGHT_CMD_BA:
	    if(p)
	    {
	        char sc[32] = {0};
	        if(line & 1)
	        {
	            p->act_1BH = value;
	            p->act_1BA = value;
	        }
	        else
	        {
	            p->act_2BH = value;
	            p->act_2BA = value;
	        }
	        HWND hWndEdit = GetDlgItem(hWnd, IDC_EDIT_RGHT_CMD_BH);
	        if(hWndEdit != HWND_INVALID)
	        {
	            GetCmdString(sc, value, TRUE);
	            SetWindowCaption(hWndEdit, sc);
	            InvalidateRect(hWndEdit, NULL, TRUE);
	        }
	        hWndEdit = GetDlgItem(hWnd, IDC_EDIT_RGHT_CMD_BA);
	        if(hWndEdit != HWND_INVALID)
	        {
	            GetCmdString(sc, value, FALSE);
	            SetWindowCaption(hWndEdit, sc);
	            InvalidateRect(hWndEdit, NULL, TRUE);
	        }
	    }
	    break;
    case IDC_EDIT_RGHT_CMD_FH:
	case IDC_EDIT_RGHT_CMD_FA:
	    if(p)
	    {
	        char sc[32] = {0};
	        if(line & 1)
	        {
	            p->act_1FH = value;
	            p->act_1FA = value;
	        }
	        else
	        {
	            p->act_2FH = value;
	            p->act_2FA = value;
	        }
	        HWND hWndEdit = GetDlgItem(hWnd, IDC_EDIT_RGHT_CMD_FH);
	        if(hWndEdit != HWND_INVALID)
	        {
	            GetCmdString(sc, value, TRUE);
	            SetWindowCaption(hWndEdit, sc);
	            InvalidateRect(hWndEdit, NULL, TRUE);
	        }
	        hWndEdit = GetDlgItem(hWnd, IDC_EDIT_RGHT_CMD_FA);
	        if(hWndEdit != HWND_INVALID)
	        {
	            GetCmdString(sc, value, FALSE);
	            SetWindowCaption(hWndEdit, sc);
	            InvalidateRect(hWndEdit, NULL, TRUE);
	        }
	    }
	    break;
	case IDC_EDIT_DUMU1:
	    if(p)
	    {
	        const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
	        if(sc)
	        {
	            p->dumu1 = atoi(sc);
	        }
	    }
	    break;
	case IDC_EDIT_DUMU2:
	    if(p)
	    {
	        const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
	        if(sc)
	        {
	            p->dumu2 = atoi(sc);
	        }
	    }
	    break;
	case IDC_EDIT_MAST_ROLL:
	    if(p)
	    {
	        const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
	        if(sc)
	        {
	            p->high_roll = atoi(sc);
	        }
	    }
	    break;
	case IDC_EDIT_AUX_ROLL:
	    if(p)
	    {
	        const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
	        if(sc)
	        {
	            p->low_roll = atoi(sc);
	        }
	    }
	    break;
	case IDC_SPEED:
	    if(p)
	    {
	        const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
	        if(sc)
	        {
	            p->speed = atoi(sc);
	        }
	    }
	    break;
	case IDC_ROCK_SPEED:
	    break;
	case IDC_YARN_STOP:
	    if(p)
	    {
	        const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
	        if(sc)
	        {
	            p->stop = atoi(sc);
	        }
	    }
	    break;
	case IDC_SINK:
	    if(p)
	    {
	        const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
	        if(sc)
	        {
	            p->sink = atoi(sc);
	        }
	    }
	    break;
	case ID_ROCK:
	    if(p)
	    {
	        p->rock.flag = value & 0x0F;
	        p->rock.shake = (value >> 4) & 0x03;
	        p->rock.drt = (value >> 6) & 0x03;
	        FlushPrgWndRock(hWnd, p);
	    }
	    break;
	case IDC_ROCK:
	    if(p)
	    {
	        p->rock.needle = value;
	        FlushPrgWndRock(hWnd, p);
	    }
	    break;
	}
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
	if(key == SCANCODE_ESCAPE)
	{
		OnBtnCmd(hWnd, ID_ESC);
	}
	else if(key == SCANCODE_PAGEUP)
	{
	    SetFocusChild(GetDlgItem(hWnd, ID_UP));
	    OnBtnCmd(hWnd, ID_UP);
	}
	else if(key == SCANCODE_PAGEDOWN)
	{
	    SetFocusChild(GetDlgItem(hWnd, ID_DOWN));
	    OnBtnCmd(hWnd, ID_DOWN);
	}
	else if(key == SCANCODE_F1)
	{
	    SetFocusChild(GetDlgItem(hWnd, ID_BTN_FLW));
	    OnBtnCmd(hWnd, ID_BTN_FLW);
	}
	else if(key == SCANCODE_F2)
	{
	    SetFocusChild(GetDlgItem(hWnd, ID_GOTO_LINE));
	    OnBtnCmd(hWnd, ID_GOTO_LINE);
	}
	else if(key == SCANCODE_F3)
	{
	    SetFocusChild(GetDlgItem(hWnd, ID_START_LINE));
	    OnBtnCmd(hWnd, ID_START_LINE);
	}
	else if(key == SCANCODE_F4)
	{
	    SetFocusChild(GetDlgItem(hWnd, ID_STOP_LINE));
	    OnBtnCmd(hWnd, ID_STOP_LINE);
	}
	else if(key == SCANCODE_F6)
	{
	    SetFocusChild(GetDlgItem(hWnd, ID_ROCK));
	    OnBtnCmd(hWnd, ID_ROCK);
	}
	else if(key == SCANCODE_ENTER)
	{
	    ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
	    if(nID == IDC_EDIT_DUMU1 || nID == IDC_EDIT_DUMU2 ||
	       nID == IDC_EDIT_MAST_ROLL || nID == IDC_EDIT_AUX_ROLL ||
	       nID == IDC_SPEED || nID == IDC_YARN_STOP || nID == IDC_SINK)
        {
            OnCreateKeyboard(hWnd, nID);
        }
	    else if(nID == IDC_ROCK)
	    {
	        WND_EXTRA_DATA data;
	        data.value = GetWindowAdditionalData(hWnd);
	        uchar rock = 0;
	        if(data.pwnd_program)
	        {
	            rock = data.pwnd_program->con.rock.needle;
	        }
	        CreateRockNumWindow(hWnd, rock);
	    }
	}
}
/*
 *
 */
static int ProgramWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
    {
    case MSG_CREATE:
        {
        	CreateProgramInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
        }
        break;
    case MSG_CLOSE:
        DestroyMainWindow(hWnd);
        MainWindowCleanup (hWnd);
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            SetWindowAdditionalData(hWnd, 0);
            if(data.pwnd_program)
            {
                free(data.pwnd_program);
            }
        }
        break;
    case MSG_KEYDOWN:
    	OnKeyDown(hWnd, wParam);
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_PROGRAM]);
        }
    	return 0;
    case MSG_COMMAND:
        {
        	OnBtnCmd(hWnd, LOWORD(wParam));
        }
    	return 0;
    case MSG_SETDATA:
        {
            OnSetKnitValue(hWnd, LOWORD(wParam), HIWORD(wParam));
        }
        return 0;
    case MSG_GOTO_LINE:
        {
            ushort src_line = GetCurrentLine(hWnd);
            ushort des_line = wParam;
            if(src_line && des_line && src_line != des_line)
            {
                FlushPrgWndLine(hWnd, des_line);
            }
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateProgramWindow(HWND hHosting)
{
    WND_EXTRA_DATA data;
    data.pwnd_program = calloc(1, sizeof(WND_PROGRAM));
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_PRG_BT_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = ProgramWinProc;
    CreateInfo.lx = 0;
    CreateInfo.ty = 0;
    CreateInfo.rx = CX_SCREEN;
    CreateInfo.by = CY_SCREEN;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = data.value;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
    	ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
